package com.yksj.healthtalk.ui.server;

import java.util.ArrayList;
import java.util.List;

import org.handmark.pulltorefresh.library.PullToRefreshBase;
import org.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import org.handmark.pulltorefresh.library.PullToRefreshListView;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.yksj.healthtalk.adapter.ServerOrderAdapter;
import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.entity.ShopOrderEntity;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.http.JsonsfHttpResponseHandler;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.utils.ToastUtil;
//订单搜素
public class ServerCenterOriderForGoodsSeachActivity extends BaseFragmentActivity implements OnClickListener,OnRefreshListener2<ListView>, OnItemClickListener{

	private Button seach_onclick;
	private EditText seach_text;
	private PullToRefreshListView mPullToRefreshListView;
	private ListView mListView;
	private ServerOrderAdapter adapter;
	private int pagenum = 1;
	private int pagecount = 20;
	private List<ShopOrderEntity> entities = new ArrayList<ShopOrderEntity>();
	private TextView buyRecord;
	private String GOODNAME;//按条件搜索名称
	private String merchant_id = "";

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.server_center_orider_base_seach);
		initView();
	}

	private void initView() {
		initTitle();
		titleTextV.setText("商品搜索");
		titleLeftBtn.setOnClickListener(this);
		seach_onclick = (Button)findViewById(R.id.seach_onclick);
		seach_onclick.setOnClickListener(this);
		seach_text = (EditText) findViewById(R.id.seach_text);
		seach_text.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				if(s.length()>0){
					seach_onclick.setVisibility(View.VISIBLE);
				}else{
					seach_onclick.setVisibility(View.GONE);
				}
			}
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}
			@Override
			public void afterTextChanged(Editable s) {
				
			}
		});
		findViewById(R.id.deleteText).setOnClickListener(this);
		mPullToRefreshListView = (PullToRefreshListView) findViewById(R.id.hall);
		mPullToRefreshListView.setOnRefreshListener(this);
		mListView = mPullToRefreshListView.getRefreshableView();
		mListView.setOnItemClickListener(this);
		mListView.setFooterDividersEnabled(true);
		buyRecord = (TextView) findViewById(R.id.buy_record);
		adapter = new ServerOrderAdapter(this, entities);
		mListView.setAdapter(adapter);
		
		
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_back:
			onBackPressed();
			break;
		case R.id.deleteText:
			seach_text.setText("");
			GOODNAME="";
			break;
		case R.id.seach_onclick:
			if(!seach_text.getText().toString().trim().equals("")){
				GOODNAME=seach_text.getText().toString().trim();
				pagenum = 1;
				entities.clear();
				adapter.notifyDataSetChanged();
				dohttp(merchant_id);
			}
			break;
		}
		
	}
	
	@Override
	public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
		pagenum = 1;
		dohttp(merchant_id);
	}

	@Override
	public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
		dohttp(merchant_id);
	}
	
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		if (1 <= arg2 && arg2 <= entities.size()) {
			Intent intent = new Intent(getApplicationContext(),ServerCenterOrderDetailListActivity.class);
			intent.putExtra("merchant_id", "");
			intent.putExtra("order_id", entities.get(arg2 - 1).getORDER_ID());
			startActivity(intent);
		}
	}
	
	private void dohttp(String merchant_id) {
		HttpRestClient.doHttpYAOFANGWANGSERVLET(merchant_id, pagenum,
				pagecount, "",GOODNAME, new JsonsfHttpResponseHandler(this) {
					@Override
					public void onSuccess(com.alibaba.fastjson.JSONObject object) {
						try {
							if (pagenum == 1)entities.clear();
							if(!"0".equals(object.getString("COUNT"))){
								entities.addAll(JSONArray.parseArray(object.getJSONArray("INFO").toString(),ShopOrderEntity.class));
								adapter.notifyDataSetChanged();
								pagenum++;
							}
							buyRecord.setText("详细记录(共"+ object.getString("COUNT") + "条)");
							if (entities.size() == 0)
								ToastUtil.showToastPanl("没有记录");
						} catch (Exception e) {
							mPullToRefreshListView.onRefreshComplete();
						}
					}


					@Override
					public void onFinish() {
						mPullToRefreshListView.onRefreshComplete();
						super.onFinish();
					}

				});
	}
}
